package com.nacos.controller;

import com.alibaba.fastjson.JSON;
import com.nacos.common.dto.UserDTO;
import com.nacos.dbservice.pojo.Users;
import com.nacos.dbservice.service.UsersService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Slf4j
public class UsersController {
    @Autowired
    UsersService usersService;
    @RequestMapping("/dbservice/userinfo/")
    public UserDTO getUserInfo(@RequestBody UserDTO userDTO) {
        log.info("请求参数={}", JSON.toJSONString(userDTO));
        Users users = usersService.selectUsersByName(userDTO.getUsername());
        log.info("用戶信息為={}", JSON.toJSONString(users));
        if (users == null) {
            return null;
        }
        UserDTO result = new UserDTO();
        BeanUtils.copyProperties(users,result);
        return result;
    }

    @RequestMapping("/dbservice/updateUserInfo/")
    @Transactional
    public void updateUserInfo(@RequestBody UserDTO userDTO) {
        log.info("updateUserInfo请求参数={}", JSON.toJSONString(userDTO));
        Users users = new Users();
        BeanUtils.copyProperties(userDTO,users);
        usersService.updateUser(users);
        log.info("更新用户信息完成");
    }
}
